/**
 * 
 */
package furniture.viewer;


import java.awt.Point;

import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;

import furniture.items3d.LayingFigure;
import furniture.items3d.model.LayingModel;

/**
 * @author balage
 *
 */
public class FurnitureViewer extends Viewer implements PaintListener{

	private final Canvas canvas;

	private Object furniture = null;
	
	/**
	 * 
	 */
	public FurnitureViewer(Composite parent, int style) {
		canvas = new Canvas(parent, style);
		canvas.addPaintListener(this);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.Viewer#getControl()
	 */
	@Override
	public Control getControl() {
		return canvas;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.Viewer#getInput()
	 */
	@Override
	public Object getInput() {
		return furniture;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.Viewer#getSelection()
	 */
	@Override
	public ISelection getSelection() {
		return new StructuredSelection();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.Viewer#refresh()
	 */
	@Override
	public void refresh() {
		canvas.redraw();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.Viewer#setInput(java.lang.Object)
	 */
	@Override
	public void setInput(Object input) {
		furniture = input;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jface.viewers.Viewer#setSelection(org.eclipse.jface.viewers.ISelection, boolean)
	 */
	@Override
	public void setSelection(ISelection selection, boolean reveal) {

	}

	@Override
	public void paintControl(PaintEvent e) {
		if (furniture != null){
			if (furniture instanceof ResourceSet){
				FurnitureModeler modeler = new FurnitureModeler();
				LayingModel model = modeler.buildModel((ResourceSet)furniture);
				
				LayingFigure.draw(e.gc, model, new Point(e.width, e.height), LayingFigure.simpleProjector);
			}
		}
		
	}

}
